package cn.fl.capital.service.impl;

import cmm.mid.core.framework.dto.DataResultDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.fl.capital.api.ICapFlowStartApiService;
import cn.fl.capital.condition.FlowUserInfoQC;
import cn.fl.capital.constant.CapitalConstant;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.capital.dto.CapFundOutApplyDTO;
import cn.fl.capital.condition.CapFundOutApplyQueryCondition;
import cn.fl.capital.api.ICapFundOutApplyApiService;
import cn.fl.capital.service.ICapFundOutApplyService;
import cn.fl.capital.transform.CapFundOutApplyVOTransform;
import cn.fl.capital.vo.CapFundOutApplyVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.constant.SysDictionaryConstant;

import java.util.List;
import java.util.Map;

/**
 * @Description: 资金池转出申请记录表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class CapFundOutApplyServiceImpl implements ICapFundOutApplyService {

	/**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(CapFundOutApplyServiceImpl.class);
	
	/**
	 * 资金池转出申请记录表 暴露服务
	 */
	@Autowired
	private ICapFundOutApplyApiService capFundOutApplyApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

	@Autowired
	private ICapFlowStartApiService capFlowStartApiService;

	@Override
	public CapFundOutApplyVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)) return null;
		CapFundOutApplyDTO dto = this.capFundOutApplyApiService.selectCapFundOutApplyById(id);
		return CapFundOutApplyVOTransform.toVO(dto);
	}

	@Override
	public Long add(CapFundOutApplyVO entity) throws BaseException{
		if(null == entity) return null;
		return this.capFundOutApplyApiService.addCapFundOutApply(CapFundOutApplyVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)) return false;
		return this.capFundOutApplyApiService.removeCapFundOutApplyByIds(ids,CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(CapFundOutApplyVO entity) throws BaseException{
		if(null == entity) return false;
		return this.capFundOutApplyApiService.modifyCapFundOutApply(CapFundOutApplyVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        CapFundOutApplyQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new CapFundOutApplyQueryCondition());
        }
		condition=(CapFundOutApplyQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.capFundOutApplyApiService.searchCapFundOutApplyListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<CapFundOutApplyVO> voList=CapFundOutApplyVOTransform.toVOList((List<CapFundOutApplyDTO>)page.getResultList());
            //this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}
	
	@Override
	public List<CapFundOutApplyVO> searchList(CapFundOutApplyQueryCondition condition) throws BaseException{
		if(null == condition) condition=new CapFundOutApplyQueryCondition();
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<CapFundOutApplyVO> voList=CapFundOutApplyVOTransform.toVOList(this.capFundOutApplyApiService.searchCapFundOutApplyList(condition));
		this.setDictData(voList);
		return voList;
	}
	
    /**
     * 添加数据字典名称
     * @param voList
     */
	public void setDictData(List<CapFundOutApplyVO> voList){
		if(CollectionUtils.isEmpty(voList)) return;
		/*Map<String, String> _CdMap = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, CapFundOutApplyConstant.CODE_CD);
		for(CapFundOutApplyVO vo:voList){
			if (_CdMap != null && _CdMap.containsKey(vo.getCd())) {
            	vo.setCdNm(_CdMap.get(vo.getCd()));
        	}	
		}*/
	}
	
	/**
     * 查询数据字典
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
    public List<KeyValueVO> searchCdList(String groupCd){
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

	@Override
	public boolean addOutApplyAndBusi(CapFundOutApplyVO entity, List<Long> intoIdList) throws BaseException {

		boolean bol = capFundOutApplyApiService.addCapFundOutApply(CapFundOutApplyVOTransform.toDTO(entity), intoIdList);
		if (bol) {
			// 开始流程
			FlowUserInfoQC qc = new FlowUserInfoQC();
			//流程业务主键id
			qc.setId(entity.getId());
			qc.setCurrentSysCd(CapitalConstant.SYSCD);
			qc.setCurrentOrgCd(CurrentThreadContext.getCurrentOrgCd());
			qc.setCurrentBuOrgCd(CurrentThreadContext.getCurrentBuOrgCd());
			qc.setCurrentOrgDispNm(CurrentThreadContext.getCurrentOrgDispNm());
			qc.setCurrentUserId(CurrentThreadContext.getCurrentUserId());
			qc.setCurrentUserName(CurrentThreadContext.getCurrentUserName());

			BaseLogUtils.info(logger, "addFlowLoanStart", "发起转来款流程请求参数是:" + JSON.toJSONString(qc));
			DataResultDTO dataResultDTO = capFlowStartApiService.addFlowLoanStart(qc);
			if ("1".equals(dataResultDTO.getCode())) {
				BaseLogUtils.error(logger, "addFlowLoanStart", "发起转来款流程请求失败！错误信息：" + dataResultDTO.getInfo());
			}
		}

		return bol;
	}

	@Override
	public Long queryTheNextPk() throws BaseException {
		return capFundOutApplyApiService.queryTheNextPk();
	}

	@Override
	public boolean modifyOutApplyAndBusi(CapFundOutApplyVO entity, List<Long> intoIdList) throws BaseException {
		return capFundOutApplyApiService.modifyCapFundOutApply(CapFundOutApplyVOTransform.toDTO(entity), intoIdList);
	}

}
